<?php
/**
* 取得当前用户权限id
* @return Integer
*/
function getPermissionsGroupId()
{
	global $db;
	$sql="select PermissionsGroupId from web_admin where admin_username='".$_SESSION['admin_user']."'";
	$rs=$db->Execute($sql);
	if($rs && $row=$rs->FetchRow())
	{
		return $row['PermissionsGroupId'];
	}
	else
	{
		return 0;
	}
}

/**
* 取得权限公用部分
* @param id		管理员id
* @return Array
*/
function getAryUserPermession()
{
	global $db,$aryUserPermession;
	if(is_array($aryUserPermession))
	{
		return $aryUserPermession;
	}
	$PermissionsGroupId=getPermissionsGroupId();		//取得当前用户用户组id
	if($PermissionsGroupId>0)
	{
		$sql="select couldview,couldnotview from admin_permissionsgroup where id='$PermissionsGroupId'";
		$rs=$db->Execute($sql);
		if($rs && $row=$rs->FetchRow())
		{
			//存储可用权限
			$param='couldview';
			$aryTemp1=explode(',',$row[$param]);		
			if(is_array($aryTemp1))
			{
				foreach($aryTemp1 as $key =>$value)
				{
					$aryUserPermession[$param][$value]=1;
				}
			}
			
			//存储不可用权限
			$param='couldnotview';
			$aryTemp1=explode(',',$row[$param]);		
			if(is_array($aryTemp1))
			{
				foreach($aryTemp1 as $key =>$value)
				{
					$aryUserPermession[$param][$value]=1;
				}
			}
			return $aryUserPermession;
		}
	}
	return '';
	
}

//权限相关函数
/**
* 根据权限字符串生成权限判断数组
* @param strPermissions	权限字符串
* @return Array
*/
function getPermissionsArray($strPermissions)
{
	if(isset($ss))
	{
		unset($ss);
	}
	if($strPermissions!='')
	{
		$arytemp=explode(',',$strPermissions);
		if(is_array($arytemp))
		{
			foreach($arytemp as $key => $value)
			{
				if($value>0)
				{
					$ss[$value]=1;
				}
			}
		}
		return $ss;
	}
	else
	{
		return '';
	}
}

/**
* 检测默认菜单
* @param str		待检测字符串
* @return Boolean
*/
function checkManuDefaut($str)
{
	$defaultMamu=array(
	'网站信息管理'
	);
	
	foreach($defaultMamu as $key => $value)
	{
		if($str==$value)
		{
			return true;
		}
	}
	return false;
}

/**
* 取得左部菜单列表
*/
function getMenuList()
{
	$ss='';
	$aryMenu1=getPermissionsByGrade();
	if(is_array($aryMenu1))
	{
		foreach($aryMenu1 as $key=>$value)
		{
			if(!checkPermission($value['permissions_id']))
			{
				continue;
			}
			if(checkManuDefaut($value['name']))
			{
				$TdDisplay='';
			}
			else
			{
				$TdDisplay='none';
			}
			$ss.='<table width="150" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>'."\n";
	  		$ss.='<td align="left" class="left_title" onclick="changedisplay(\'list_'.$key.'\')" style="cursor:pointer">'.$value['name'].'</td>'."\n";
			$ss.='</tr>'."\n";
			$ss.=' <tr>'."\n";
	  		$ss.='<td id="list_'.$key.'" style="display:'.$TdDisplay.'" bgcolor="#FFFFFF">'.getPermissionsListByPid($value['permissions_id']).'</td>'."\n";
	  		$ss.='</tr>'."\n";	
			$ss.='<tr>
				<td class="left_tb_b"></td>
			</tr>'."\n";		
			$ss.='</table>'."\n";
		}
		
	}
	return $ss;
}

//根据上级id取得菜单列表
function getPermissionsListByPid($pid)
{
	$ss='';
	$aryMenu1=getPermissionsByPid($pid);
	if(is_array($aryMenu1))
	{
		$ss.='<table width="100%" border="0" cellpadding="0" cellspacing="0">'."\n";
      
		foreach($aryMenu1 as $key=>$value)
		{
			if(!checkPermission($value['permissions_id']))
			{
				continue;
			}
			$ss.='<tr>'."\n";
	  		$ss.='<td align="left" class="left_tb_c"><img src="images/img02.jpg" width="10" height="12" />&nbsp;<a href="'.WEBDOMAIN.$value['link'].'" target="mainFrame">'.$value['name'].'</a></td>'."\n";
			$ss.='</tr>'."\n";
		}
		$ss.='</table>'."\n";
	}
	return $ss;
}

/**
* 取得grade=1的权限信息
* @return Array
*/
function getPermissionsByGrade()
{
	global $db;
	$sql="select * from admin_permissions where parent_id=0 order by permissions_id asc";
	$rs=$db->Execute($sql);
	$i=0;
	while($rs && $row=$rs->FetchRow())
	{
		$ss[$i]['name']=$row['name'];
		$ss[$i]['link']=$row['link'];
		$ss[$i]['permissions_id']=$row['permissions_id'];
		$i++;
	}
	return $ss;
}


//根据上级权限id取得权限
function getPermissionsByPid($pid)
{
	global $db;
	$sql="select * from admin_permissions where parent_id = '$pid' order by permissions_id asc";
	$rs=$db->Execute($sql);
	$i=0;
	while($rs && $row=$rs->FetchRow())
	{
		$ss[$i]['name']=$row['name'];
		$ss[$i]['link']=$row['link'];
		$ss[$i]['permissions_id']=$row['permissions_id'];
		$i++;
	}
	return $ss;
}

/**
* 根据权限数组检测是否具备该权限
* @return boolean
*/
function checkPermission($permissions_id)
{
	global $supperadmin;
	global $aryUserPermession;		////权限存储变量，在/manage/Permissions.php中定义
	if($_SESSION['admin_user']==$supperadmin)
	{
		return true;
	}
	if($aryUserPermession['couldnotview'][$permissions_id]==1)
	{
		return false;
	}
	if($aryUserPermession['couldview'][$permissions_id]==1)
	{
		return true;
	}
	global $db;
	$sql="select isview from admin_permissions where permissions_id='$permissions_id'";
	$rs=$db->Execute($sql);
	if($rs && $row=$rs->FetchRow())
	{
		if($row['isview']==1)
		{
			return true;
		}
	}
	return false;
}
function checkPermission_admin($permissions_id,$aryPermissions){
  if (isset($aryPermissions)) {
	if($aryPermissions[$permissions_id]==1){
	  return true;
	} else {
	  return false;
	}
  } else {
  	return false;
  }
}
?>